package com.pzsh.service.impl;

import com.pzsh.entity.*;
import com.pzsh.mapper.*;
import com.pzsh.service.ITaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Param:
 * @return:
 * @Author: GXL
 * @Date: 2019/12/09
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class TaskServiceImpl implements ITaskService {

    @Autowired
    private RewardTaskMapper rewardTaskMapper;
    @Autowired
    private UserPackMapper userPackMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TransactionssMapper transactionssMapper;
    @Autowired
    private CensusMapper censusMapper;
    @Transactional(propagation=Propagation.REQUIRED)
    @Override
    public void RewardTask() {
        Long nowTime = new Date().getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Long rewardDateForFind = Long.valueOf(dateFormat.format(nowTime));

        RewardTaskEntity rewardTaskEntity = new RewardTaskEntity();
        rewardTaskEntity.setStatus(0);
        rewardTaskEntity.setRewardDateForFInd(rewardDateForFind);
        int count = rewardTaskMapper.selectCount(rewardTaskEntity);
        int rows = 1000;
        int exceTimes = (count / rows) + 1;//每次limit 1000 ， 执行excetimes次；
        for (int i = 0; i < exceTimes; i++){
            int offset = i * rows;
            RowBounds rowBounds = new RowBounds(offset, rows);
            List<RewardTaskEntity> list = rewardTaskMapper.selectByRowBounds(rewardTaskEntity, rowBounds);
            for (RewardTaskEntity entity : list) {
                UserEntity userEntity = new UserEntity();
                userEntity.setUserId(entity.getUserId());
                userEntity = userMapper.selectOne(userEntity);
                //更新奖励状态
                entity.setStatus(1);
                rewardTaskMapper.updateByPrimaryKey(entity);

                //添加用户冻结积分（后期转pzsh）
                UserPackEntity userPackEntity = new UserPackEntity();
                userPackEntity.setUserId(entity.getUserId());
                userPackEntity = userPackMapper.selectOne(userPackEntity);
                userPackEntity.setUnuseIntegral(userPackEntity.getUnuseIntegral() + entity.getRewardPs());
                userPackMapper.updateByPrimaryKey(userPackEntity);

                //更新累计已到账的奖励ps
                CensusEntity censusEntity = censusMapper.selectAll().get(0);
                censusEntity.setCountRewardArrive(censusEntity.getCountRewardArrive() + entity.getRewardPs());
                censusMapper.updateByPrimaryKey(censusEntity);

                //添加用户奖励ps收益明细记录
                TransactionEntity transactionEntity = new TransactionEntity();
                transactionEntity.setUserId(userEntity.getUserId());
                transactionEntity.setAccount(userEntity.getAccount());
                transactionEntity.setInoutType(1);
                transactionEntity.setRecordType(0);
                transactionEntity.setTType(15);
                transactionEntity.setRemark("商城消费奖励PS积分");
                transactionEntity.setNumb(entity.getRewardPs());
                transactionEntity.setCreateDate(new Timestamp(nowTime));
                transactionEntity.setModifyDate(new Timestamp(nowTime));
                transactionssMapper.insert(transactionEntity);
            }
        }
    }
}
